package org.ling.xuan.action;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.config.TestDbConfig;
import org.ling.xuan.context.AddPipeLineContext;
import org.ling.xuan.dao.FileDao;
import org.ling.xuan.dao.LocalAddDao;
import org.ling.xuan.dao.ProjectDao;
import org.ling.xuan.dao.SettingDao;
import org.ling.xuan.dialog.SettingDialog;
import org.ling.xuan.entity.mo.CommonSetting;
import org.ling.xuan.entity.mo.LocalAdd;
import org.ling.xuan.entity.mo.ProjectSetting;
import org.ling.xuan.entity.mo.Setting;
import org.ling.xuan.pipeline.PipeLineFactory;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxOptional;

import java.util.ArrayList;
import java.util.List;


/**
 * 检测单个文件
 *
 * @author liting50684
 * @date 2023/08/21
 */
public class LxCheckAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
        LoggerUtil.log(e.getProject(), "file check:{}", filePath);
        CommonSetting cs = FileDao.get(CommonSetting.class, "CommonSetting.json", new CommonSetting());
        ProjectSetting ps = ProjectDao.getByFilePath(filePath);
        if (cs == null || StrUtil.isBlank(cs.getAuth()) || ps == null || StrUtil.isBlank(ps.getProjectPath())) {
            Project project = e.getProject();
            String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
            Setting setting = null;
            setting = SettingDao.getSetting(e.getProject(), basePath);
            SettingDialog settingDialog = new SettingDialog(project, true, setting);
            if (settingDialog.showAndGet()) {
                SettingDao.updateSetting(basePath, settingDialog.getSetting());
                TestDbConfig.refresh();
            }
        } else {
            LocalAdd localAdd = LocalAddDao.getCur();
            AddPipeLineContext context = PipeLineFactory.buildContext(e.getProject(),ps,cs,filePath, localAdd);
            PipeLineFactory.pipelineInstance().start(context);
            List<Integer> warnIndex = new ArrayList<>();
            List<Integer> errorIndex = new ArrayList<>();
            for (int i = 0; i < context.getCheckStatus().size(); i++) {
                if (context.getCheckStatus().get(i) == 0) {
                    errorIndex.add(i);
                } else if (context.getCheckStatus().get(i) == 2) {
                    warnIndex.add(i);
                }
            }

            LoggerUtil.log(e.getProject(), "------------------------检查结果---------------------------");
            LoggerUtil.log(e.getProject(), "文件名称:{}",filePath);
            LoggerUtil.log(e.getProject(), "警告数量:{},错误数量:{}", warnIndex.size(), errorIndex.size());
            if (warnIndex.size() > 0) {
                LoggerUtil.log(e.getProject(), "警告详情:");
                warnIndex.forEach(f -> LoggerUtil.log(e.getProject(), "        " + context.getMessages().get(f)));
            }
            if (errorIndex.size() > 0) {
                LoggerUtil.log(e.getProject(), "错误详情:");
                errorIndex.forEach(f -> LoggerUtil.log(e.getProject(), "        " + context.getMessages().get(f)));
            }
        }
    }

    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().checkCondition(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
